package com.ztesoft.huangfu.learning.springboot.springbootswagger2.controller;

import com.ztesoft.huangfu.learning.springboot.springbootcommon.base.BaseResponse;
import com.ztesoft.huangfu.learning.springboot.springbootswagger2.entity.User;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Author: huangfu.daohui
 * Date: 2018/6/3
 * Time: 13:54
 * Version: 1.0
 * Description: api文档，遇到一个坑，不能命名为swagger2Controller,冲突
 * https://blog.csdn.net/saytime/article/details/74937664
 *
 *  http://localhost:8089/swagger-ui.html
 *  http://10.45.28.231:8083/v2/api-docs json 文件
 **/
@RestController
@RequestMapping("/swagger2")
public class Swagger2APIController {

    // 创建线程安全的Map
    private static Map<Long, User> users = Collections.synchronizedMap(new HashMap<Long, User>());

    @ApiOperation(value = "第一个restful的API", notes = "first helloworld")
    @GetMapping("/hi")
    public String helloWorld() {
        return "hello world";
    }

    @ApiOperation(value = "获取用户列表", notes = "获取用户列表")
    @GetMapping("/get")
    public BaseResponse<List<User>> getUserList() {
        // 处理"/users/"的GET请求，用来获取用户列表
        // 还可以通过@RequestParam从页面中传递参数来进行查询条件或者翻页信息的传递
        List<User> r = new ArrayList<>(users.values());
        return new BaseResponse<>(true, "查询列表成功", r);
    }

    @ApiOperation(value = "创建用户", notes = "根据User对象创建用户")
    @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
    @PostMapping("/add")
    public BaseResponse<User> postUser(User user) {
        // 处理"/users/"的POST请求，用来创建User
        // 除了@ModelAttribute绑定参数之外，还可以通过@RequestParam从页面中传递参数
        users.put(user.getId(), user);
        return new BaseResponse<>(true, "新增成功", users.get(user.getId()));
    }

    @ApiOperation(value = "获取用户详细信息", notes = "根据url的id来获取用户详细信息")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Integer", paramType = "path")
    @GetMapping("/getById")
    public BaseResponse<User> getUser(Long id) {
        // 处理"/users/{id}"的GET请求，用来获取url中id值的User信息
        // url中的id可通过@PathVariable绑定到函数的参数中
        return new BaseResponse<>(true, "查询成功", users.get(id));
    }

    @ApiOperation(value = "更新信息", notes = "根据url的id来指定更新用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "user", value = "用户实体user", required = true, dataType = "User")
    })
    @PutMapping("/updateById")
    public BaseResponse<String> putUser(Long id, User user) {
        // 处理"/users/{id}"的PUT请求，用来更新User信息
        User u = users.get(id);
        u.setName(user.getName());
        u.setAge(user.getAge());
        users.put(id, u);
        return new BaseResponse<>(true, "更新成功", "");
    }

    @ApiOperation(value = "删除用户", notes = "根据url的id来指定删除用户")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long", paramType = "path")
    @DeleteMapping("/deleteById")
    public BaseResponse<String> deleteUser(Long id) {
        // 处理"/users/{id}"的DELETE请求，用来删除User
        users.remove(id);
        return new BaseResponse<>(true, "删除成功", "");
    }



}
